package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.bean.jsonbean.ResAddBabyCheckBean;
import com.rjwh.dingdong.client.bean.localbean.AddBabyCheckData;
import com.rjwh.dingdong.client.bean.localbean.ChildInfo;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.AddBabyBoundModel;
import com.rjwh.dingdong.client.mvp.presenter.IAddBabyBoundPresenter;
import com.rjwh.dingdong.client.mvp.view.AddBabyBoundView;
import com.rjwh.dingdong.client.util.ToastUtil;

/**
 * @author Kay
 * @ClassName AddBabyBoundPresenter
 * @Description
 * @date 2016-7-21
 * @history 2016-7-21 author: description:
 */
public class AddBabyBoundPresenter extends BasePresenter<AddBabyBoundView> implements IAddBabyBoundPresenter {

    private AddBabyBoundModel model;

    public AddBabyBoundPresenter(Context context, AddBabyBoundView mMvpView) {
        super(context, mMvpView);
        model = new AddBabyBoundModel(this);
    }

    /**
     * 添加宝宝验证
     *
     * @param childid
     */
    @Override
    public void doAddBabyCheck(String childid) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.doAddBabyCheck(childid);
        }
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            getMvpView().dismissLoading();
            switch (businessType) {
                case NetConstant.ADDBABYCHECK:
                    ResAddBabyCheckBean resAddBabyCheckBean = (ResAddBabyCheckBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resAddBabyCheckBean != null) {
                        AddBabyCheckData data = resAddBabyCheckBean.getData();
                        if (data != null) {
                            ChildInfo childInfo = data.getChildInfo();
                            if (childInfo != null) {
                                getMvpView().LoadData(childInfo);
                            }
                        }
                    } else if (!AsStrUtils.isEmpty(mAsResult.getResultMsg())) {
                        ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                    }
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

}
